<template>
  <div class="nav-bar">

    <div class="nav-left">
      <img src="~assets/img/puma.png" alt="">
      <h1>PUMA</h1>
    </div>

    <div class="nav-right">
      <div class="search-area">
        <input type="text"
               class="search"
               v-model="searchKeyWords"
               placeholder="搜索你想要的商品......"/>
        <div class="search-list">
          <div class="search-list-item" v-for="i in preSearch" @click="preSearchClick(i.name)">
            {{i.name}}------[{{i.goods_category_name}}]
          </div>
          <div class="search-list-item cancel" v-if="preSearch.length > 0" @click="() => {this.preSearch = []}">x</div>
        </div>
        <button class="search-btn" @click="searchClick"><h2>搜   索</h2></button>
      </div>
      <div class="classification">
        <div class="classification-item" @click="classifyClick(0)">首页</div>
        <div class="classification-item" v-for="item in rootCategory" @click="classifyClick(item.id)">{{item.name}}</div>

      </div>
    </div>

  </div>
</template>

<script>
  import MenuBar from "components/common/menubar/MenuBar";

  import {debounce} from 'common/utils'

  import {getGoodsList} from 'network/mix';

  export default {
    name: "NavBar",
    components: {
      MenuBar,
      // Search,
    },
    props: {
      rootCategory: {
        type: Array,
        default() {
          return []
        }
      },
    },
    data() {
      return {
        searchKeyWords: '', // 搜索关键字
        preSearch: [], // 预搜索列表
      }
    },
    watch: {
      searchKeyWords() { // 如果searchKeyWords发生改变
        // console.log('改变了' + this.searchKeyWords);
        this.searchChange()
        // const debounceSearchChange = debounce(this.searchChange, 3000) //对sc内封装的刷新BetterScroll函数mRefresh进行防抖动处理,得到一个新的防抖动处理后的函数debounceRefresh,但是注意不要传入括号

        // debounceSearchChange()
      }
    },
    methods: {
      searchChange() { // 当搜索框发生改变
        this.preSearch = []
        if (this.searchKeyWords !== '') {

          getGoodsList(0, 0, 0, '', this.searchKeyWords, 0)
            .then(res => {
              // console.log(res);
              this.preSearch = res.page.list

              // this.isPreSearchShow = this.preSearch.length > 0;
              // if (this.preSearch.length > 0) { // 如果有数据
              //   this.isPreSearchShow = true
              // } else { // 反之，如果没有数据
              //   this.isPreSearchShow = false
              // }
            })
            .catch(error => {
              console.log('获取预搜索商品列表失败');
              console.log(error);
            })
        }
      },
      searchClick() { // 当点击搜索按钮或者点击预搜索项
        this.$emit('searchClickEmit', this.searchKeyWords)
        this.preSearch = []
      },
      preSearchClick(name) { // 当预搜索列表被点击
        this.$emit('searchClickEmit', name) // 发生搜索事件
        this.preSearch = [] // 预搜索列表置空
        this.searchKeyWords = '' // 将searchKeyWords变成显示的name
      },
      classifyClick(id) { // 当点击商品分类
        this.$emit('classifyClickEmit', id)
      }
    }
  }
</script>

<style scoped>
  .nav-bar {
    position: relative;

    height: 200px;
    width: 100%;
    box-shadow: 0 0 1px rgba(100, 100, 100, 0.8);

    background-color: #ffffff;
  }
  .nav-left {
    float: left;

    margin-left: 3%;
    margin-top: 1%;

    /*background-color: green;*/

    width: 20%;
    height: 60%;

    text-align: center;
    color: red;
  }
  .nav-left img {
    width: 100%;
    height: 100%;
  }

  .nav-right {
    /*position: absolute;*/
    float: right;
    /*background-color: red;*/
    width: 77%;
    height: 100%;

    overflow: hidden;

   }
  .search-area {
    /*position: absolute;*/
    /*background-color: black;*/
    padding-top: 5%;
    padding-left: 15%;

  }
  .search {
    width: 60%;
    height: 40px;
    outline: none;
    border: 2px solid red;
  }
  .search:focus { /**选中搜索框*/
    outline: none;/**先去除原轮廓*/
    border: 1px solid red;/**设置为黑色的边框线*/
    border-radius: 4px;/**圆角4px*/
  }
  .search-btn { /**搜索按钮的样式*/
    position: absolute;

    width: 100px;
    height: 44px;
    color: #ffffff;
    background-color: red;
    border: none;/**先去除原轮廓*/
    outline: none; /**先去除原轮廓*/
  }
  .search-btn:hover { /**当点击搜索按钮*/
    cursor: pointer;
    opacity: 0.75;
  }
  .search-list {
    position: absolute;
    width: 705px;
    /*height: 200px;*/

    z-index: 9;
    background-color: white;
  }
  .search-list .search-list-item {
    height: 30px;
    line-height: 30px;
    /*border-right: 1px solid #cdcdcd;*/
    /*border-left: 1px solid #cdcdcd;*/
    /*border-bottom: 1px solid #cdcdcd;*/
  }
  .search-list .search-list-item:hover {
    cursor: pointer;
    opacity: 0.75;
    background-color: #e3e3e3;
  }
  .cancel {
    text-align: center;
  }

  .classification {
    position: absolute;
    /*padding-top: 2%;*/
    margin-left: 1%;

    bottom: 3%;

    /*background-color: yellow;*/

    width: 70%;
  }
  .classification-item {
    float: left;
    margin-right: 3%;

    font-size: 20px;

    /*background-color: blue;*/
  }
  .classification-item:hover {
    color: red;
    cursor: pointer;
  }


</style>